import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../states/BlueListController.dart' show BlueListController,BlueDevice;

class MDropdownBtn extends StatefulWidget {
  const MDropdownBtn({super.key});
  @override
  State<MDropdownBtn> createState() => _MDropdownBtnState();
}

const firstVal='请选择';
class _MDropdownBtnState extends State<MDropdownBtn> {
  String dropdownValue='';

  @override
  Widget build(BuildContext context) {
    //widget.list.add(firstVal);
    final BlueListController blueList = Get.find();
    if(dropdownValue.isEmpty){
      dropdownValue=blueList.blues.first.localName;
    }
    return Obx(() => 
      DropdownButton<String>(
        value: dropdownValue,
        icon: const Icon(Icons.arrow_downward),
        elevation: 16,
        style: const TextStyle(color: Colors.deepPurple),
        underline: Container(
          height: 2,
          color: Colors.deepPurpleAccent,
        ),
        onChanged: (String? val) {
          setState(() {
            dropdownValue = val as String;
            final blue=blueList.blues.firstWhere((ele) => ele.localName==val);
            blueList.setBlueDevice(blue);
          });
        },
        items: blueList.blues.map<DropdownMenuItem<String>>((BlueDevice val) {
          return DropdownMenuItem<String>(
            value: val.localName,
            child: Text(val.localName),
          );
        }).toList(),
      )
    );
  }
}